﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LNUGame.View.ParticleSystemBloodSplatter
{
    /// <summary>
    /// Particle emitter to represent blood splattering from an injured character
    /// </summary>
    class BloodSplatterSystem
    {
        private const int NUM_PARTICLES = 30;
        private BloodSplatterParticle[] m_splatter = new BloodSplatterParticle[NUM_PARTICLES];

        private readonly Random m_rand;
        private const float BASE_SPEED = 50.0f;

        /// <summary>
        /// Default constructor.
        /// Generates a defined amount of splatter particles to be displayed and stores them in an array.
        /// </summary>
        public BloodSplatterSystem()
        {
            m_rand = new Random(100);
            GenerateNewSplatterParticles();
        }

        /// <summary>
        /// Generates a defined amount of splatter particles to be displayed and stores them in an array.
        /// Gives each splatter particle a velocity and lifetime.
        /// </summary>
        internal void GenerateNewSplatterParticles()
        {
            for (int i = 0; i < NUM_PARTICLES; i++)
            {
                Vector2 velocity = new Vector2((2.0f * (float)m_rand.NextDouble() - 1.0f), (2.0f * (float)m_rand.NextDouble() - 1.0f));

                velocity *= BASE_SPEED * (float)m_rand.NextDouble();

                float lifetime = (float)m_rand.NextDouble() * 7;

                m_splatter[i] = new BloodSplatterParticle(velocity, lifetime);
            }
        }

        
        /// <summary>
        /// Draws the bloodsplatter to the screen.
        /// The longer a splatter particle have lived, the more transperant i gets.
        /// </summary>
        /// <param name="a_spriteBatch">The SpriteBatch we use to draw with</param>
        /// <param name="a_texture">The texture we want to use for the raindrop</param>
        /// <param name="a_elapsedTime">How much time have elapsed since the game was started</param>
        /// <param name="a_spawnPosition">The viewposition the splatter should be drawn on</param>
        internal void DrawSplatter(SpriteBatch a_spriteBatch, Texture2D a_texture, float a_elapsedTime, Vector2 a_spawnPosition)
        {
            for (int i = 0; i < NUM_PARTICLES; i++)
            {
                m_splatter[i].Update(a_elapsedTime);

                Vector2 modelPosition = m_splatter[i].Position;

                Vector2 viewPosition = modelPosition+ a_spawnPosition + new Vector2(3f, -20f);
                int particleViewSize = 25;

                Rectangle destinationRectangle = new Rectangle((int)viewPosition.X - particleViewSize / 2, (int)viewPosition.Y - particleViewSize / 2, particleViewSize, particleViewSize);

                float opacity = m_splatter[i].Opacity;
                Color color = new Color(opacity, opacity, opacity, opacity);

                a_spriteBatch.Draw(a_texture, destinationRectangle, color);
            }
        }
    }
}
